<template>
  <div class="team-assessment">
    <!-- 顶部标签页 -->
    <div class="tabs-container">
      <div
        v-for="tab in tabs"
        :key="tab.key"
        :class="['tab-item', { active: activeTab === tab.key }]"
        @click="activeTab = tab.key"
      >
        {{ tab.label }}
      </div>
    </div>

    <!-- 新建项目团队按钮 -->
    <div class="action-bar">
      <el-button type="primary" @click="handleCreateTeam">
        新建项目团队
      </el-button>
    </div>

    <!-- 团队列表 -->
    <div class="team-list">
      <el-table :data="teamData" style="width: 100%" v-loading="loading">
        <el-table-column prop="name" label="中文名称" width="300" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="createTime" label="创建时间" width="180"/>
        <el-table-column label="操作" width="400" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleAddMember(scope.row)">
              添加人员
            </el-button>
            <el-button type="success" size="small" @click="handleViewMembers(scope.row)">
              团队成员
            </el-button>
            <el-button type="info" size="small" @click="handleViewReports(scope.row)">
              团队报告
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>

      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 新建团队对话框 -->
    <el-dialog v-model="dialogVisible" title="新建项目团队" width="500px">
      <el-form :model="teamForm" :rules="teamRules" ref="teamFormRef" label-width="100px">
        <el-form-item label="团队名称" prop="name">
          <el-input v-model="teamForm.name" placeholder="请输入项目团队名称，如：产品研发团队" />
        </el-form-item>
        <el-form-item label="团队描述" prop="description">
          <el-input
            v-model="teamForm.description"
            type="textarea"
            rows="3"
            placeholder="请输入团队职责描述，包括主要工作内容、团队角色等"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()

// 响应式数据
const loading = ref(false)
const dialogVisible = ref(false)
const activeTab = ref('current')
const teamFormRef = ref()

// 标签页数据
const tabs = [
  { key: 'current', label: '项目团队' },
  { key: 'completed', label: '已结束团队' }
]

// 团队表单
const teamForm = reactive({
  name: '',
  description: ''
})

// 表单验证规则
const teamRules = {
  name: [
    { required: true, message: '请输入团队名称', trigger: 'blur' },
    { min: 2, max: 20, message: '团队名称长度在2-20个字符之间', trigger: 'blur' }
  ],
  description: [
    { required: true, message: '请输入团队描述', trigger: 'blur' },
    { min: 10, max: 200, message: '团队描述长度在10-200个字符之间', trigger: 'blur' }
  ]
}

// 分页数据
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 团队数据
const teamData = ref([
  {
    id: 1,
    name: '产品研发团队',
    description: '负责核心产品的研发与技术创新，包含前端、后端、UI/UX等多个角色',
    createTime: '2024-08-13 16:23:06'
  },
  {
    id: 2,
    name: '市场营销团队',
    description: '负责市场推广、品牌建设和客户关系维护，推动业务增长',
    createTime: '2024-08-12 14:36:05'
  },
  {
    id: 3,
    name: '项目管理团队',
    description: '负责项目规划、进度管控和跨部门协调，确保项目顺利交付',
    createTime: '2024-08-12 18:03:11'
  }
])

// 方法
const handleCreateTeam = () => {
  dialogVisible.value = true
  // 重置表单
  teamForm.name = ''
  teamForm.description = ''
}

const handleSubmit = async () => {
  try {
    await teamFormRef.value.validate()
    // 这里添加创建团队的API调用
    console.log('创建团队:', teamForm)

    // 模拟添加数据
    const newTeam = {
      id: Date.now(),
      name: teamForm.name,
      description: teamForm.description,
      createTime: new Date().toLocaleString('zh-CN')
    }
    teamData.value.unshift(newTeam)
    pagination.total++

    dialogVisible.value = false
    ElMessage.success(`团队"${teamForm.name}"创建成功！`)
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}

const handleAddMember = (row) => {
  ElMessage.info(`正在为"${row.name}"添加团队成员...`)
  console.log('添加人员到团队:', row)
}

const handleViewMembers = (row) => {
  router.push(`/dashboard/assessment/team/${row.id}/members`)
}

const handleViewReports = (row) => {
  router.push(`/dashboard/assessment/team/${row.id}/reports`)
}

const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除团队"${row.name}"吗？删除后将无法恢复该团队的所有数据。`,
      '确认删除团队',
      {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )

    // 模拟删除
    const index = teamData.value.findIndex(item => item.id === row.id)
    if (index > -1) {
      teamData.value.splice(index, 1)
      pagination.total--
      ElMessage.success(`团队"${row.name}"删除成功！`)
    }
  } catch {
    // 用户取消删除
  }
}

const handleSizeChange = (val) => {
  pagination.pageSize = val
  loadTeamData()
}

const handleCurrentChange = (val) => {
  pagination.currentPage = val
  loadTeamData()
}

const loadTeamData = () => {
  loading.value = true
  // 这里添加加载数据的API调用
  setTimeout(() => {
    pagination.total = teamData.value.length
    loading.value = false
  }, 500)
}

onMounted(() => {
  loadTeamData()
})
</script>

<style scoped>
.team-assessment {
  background: white;
  border-radius: 8px;
  padding: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.tabs-container {
  display: flex;
  border-bottom: 1px solid #e5e7eb;
  background: white;
  border-radius: 8px 8px 0 0;
}

.tab-item {
  padding: 16px 32px;
  cursor: pointer;
  color: #6b7280;
  font-weight: 500;
  border-bottom: 3px solid transparent;
  transition: all 0.3s;
  position: relative;
}

.tab-item:first-child {
  border-radius: 8px 0 0 0;
}

.tab-item.active {
  color: white;
  background: #5a67d8;
  border-bottom-color: #5a67d8;
}

.tab-item:hover:not(.active) {
  background: #f3f4f6;
  color: #374151;
}

.action-bar {
  padding: 20px 24px;
  background: #f8fafc;
  border-bottom: 1px solid #e5e7eb;
}

.team-list {
  padding: 24px;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 24px;
  padding-top: 24px;
  border-top: 1px solid #e5e7eb;
}

:deep(.el-table th) {
  background: #f8fafc !important;
  color: #374151;
  font-weight: 600;
}

:deep(.el-table td) {
  padding: 16px 0;
}

:deep(.el-button + .el-button) {
  margin-left: 8px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}
</style>
